﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;

namespace SPVisualDev
{
    public class RedirectHttpModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(application_BeginRequest);
        }

        private void application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;

            string req_url = context.Request.Url.ToString().ToLower();

            try
            {
                if (req_url.IndexOf("/12/") == -1)
                {
                    context.Response.Redirect("http://" + context.Request.Url.Host);
                    return;
                }

                string rel_12_path = req_url.Substring(req_url.IndexOf("/12/"));
                string sp_web_url = req_url.Substring(0, req_url.IndexOf("/spvisualdev/"));

                if (rel_12_path.StartsWith("/12/template/layouts"))
                {
                    string s = rel_12_path.Substring(20);
                    context.Response.Redirect(sp_web_url + "/_layouts" + s, false);
                }
            }
            catch { }
        }

        public void Dispose()
        {
        }
    }
}
